/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.fd.client;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;


import com.xy.biz.fd.domain.req.repository.GetRepositoryRequest;
import com.xy.biz.fd.domain.req.repository.ListRepositoryRequest;
import com.xy.biz.fd.domain.resp.GetDocumentResponse;
import com.xy.biz.fd.domain.resp.GetRepositoryResponse;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.util.ResultUtils;
import org.springframework.util.CollectionUtils;

/**
 *
 *
 * @author ssl
 * @date 2021-1-18
 */
public interface RepositoryClient {

    /**
     * 查找一个
     *
     * @param request
     * @return
     */
    Result<GetRepositoryResponse> getRepository(GetRepositoryRequest request);

    /**
     * 查找多个
     *
     * @param collection 标识集合
     * @return
     */
    Result<List<GetRepositoryResponse>> listRepositoryById(Collection<Long> collection);

    /**
     * 查找多个
     *
     * @param request 查找条件
     * @return
     */
    Result<List<GetRepositoryResponse>> listRepository(ListRepositoryRequest request);

    /**
     * 统计个数
     *
     * @param request 查找条件
     * @return
     */
    Result<Integer> countRepository(ListRepositoryRequest request);

    /**
     * 分页查找
     *
     * @param request 查找条件
     * @return
     */
    Result<Page<GetRepositoryResponse>> pageRepository(ListRepositoryRequest request);

    /**
     * 获取发行知识库信息
     * @return
     */
    Result<GetRepositoryResponse> getReleaseRepositoryInfo();


    default <T> void buildByRepository(Collection<T> source,
                                     Collection<Long> repositoryIds,
                                     Function<T, Long> repositoryIdGetter,
                                     BiConsumer<T, GetRepositoryResponse> builder
    ) {
        Objects.requireNonNull(repositoryIdGetter, "The userIdGetter is required.");
        Objects.requireNonNull(builder, "The builder is required.");
        if (CollectionUtils.isEmpty(source)) {
            return;
        }
        if (CollectionUtils.isEmpty(repositoryIds)) {
            return;
        }
        Set<Long> userIdSet = new HashSet<>(repositoryIds);
        Result<List<GetRepositoryResponse>> listResult = listRepositoryById(userIdSet);
        List<GetRepositoryResponse> getRepositoryResponses = ResultUtils.get(listResult);
        if (!CollectionUtils.isEmpty(getRepositoryResponses)) {
            Map<Long, GetRepositoryResponse> repositoryResponseMap =
                    getRepositoryResponses.stream().collect(Collectors.toMap(resp -> resp.getRepositoryId(), resp -> resp));
            for (T t : source) {
                Long documentId = repositoryIdGetter.apply(t);
                GetRepositoryResponse getRepositoryResponse = repositoryResponseMap.get(documentId);
                if (!Objects.isNull(getRepositoryResponse)) {
                    builder.accept(t, getRepositoryResponse);
                }
            }
        }
    }
}
